package cn.tedu.autoshow.website.controller;

import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.web.JsonResult;
import cn.tedu.autoshow.website.pojo.param.BannerAddNewParam;
import cn.tedu.autoshow.website.pojo.param.BannerUpdateInfoParam;
import cn.tedu.autoshow.website.pojo.vo.BannerListItemVO;
import cn.tedu.autoshow.website.pojo.vo.BannerStandardVO;
import cn.tedu.autoshow.website.pojo.vo.TicketListListItemVO;
import cn.tedu.autoshow.website.service.IBannerService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import static cn.tedu.autoshow.commons.constants.StatusConstants.PAY_REFUND;

@RestController
@RequestMapping("/banners")
@Validated
@Slf4j
@Api(tags = "01. 轮播图管理模块")
public class BannerController {

    @Autowired
    IBannerService service;

    @PostMapping("/add-new")
    @ApiOperation("新增轮播图")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BannerAddNewParam bannerAddNewParam,
                             @ApiIgnore HttpSession session){

        service.addNew(bannerAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除轮播图")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "Long")
    public JsonResult deleteById(
            @Range(min = 1, message = "轮播图ID不合法") @PathVariable Long id,
            @ApiIgnore HttpSession session){

        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID更新轮播图")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "Long")
    public JsonResult deleteById(
            @Range(min = 1, message = "轮播图ID不合法") @PathVariable Long id,
            @Valid BannerUpdateInfoParam bannerUpdateInfoParam, @ApiIgnore HttpSession session){

        service.updateInfoById(id, bannerUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询轮播图详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询轮播图失败，请提交合法的ID值！")
            @PathVariable Long id){
        BannerStandardVO standardById = service.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/type/{typeId:[0-9]+}")
    @ApiOperation("根据轮播图类别-查询轮播图列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")})
    public JsonResult listByTypeId(
            @Range(min = 1, message = "类别ID不合法") @PathVariable Integer typeId,
            @Range(min = 1, message = "查询轮播图列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<BannerListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = service.listByTypeId(typeId, pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.listByTypeId(typeId, pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @GetMapping("")
    @ApiOperation("查询轮播图列表列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")})
    public JsonResult list(
            @Range(min = 1, message = "查询轮播图列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<BannerListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

}
